<?php
namespace Admin\Controller;
use Think\Controller;
class SystemConfigController extends CommonController {
    public function system_config(){
        //查询系统配置信息
        $configInfo = M('system_config')->where(array('config_id'=>array('not in','3')))->select();
        $maintenanceInfo = M('system_config')->where(array('config_id'=>'3'))->find();
        if(!$_POST){
            $ptitle = '系统配置';
            $title = '首页';

            $this->assign('configInfo',$configInfo);
            $this->assign('maintenanceInfo',$maintenanceInfo);
            $this->assign('ptitle',$ptitle);
            $this->assign('title',$title);
            $this->display();
        }else{
            foreach($configInfo AS $k => $v)
            {
                $old_config_name = 'old_'.$v['config_name'];
                $old_config_value = I($old_config_name);//隐藏域中的原来值
                $config_value = I($v['config_name']);//隐藏域中的现在值
                //如果原来值不等于现在的值，执行更改操作
                M()->execute('begin');
                if($old_config_value != $config_value){
                    $editResult = M('system_config')->where(array('config_name' => $v['config_name']))->save(array('config_value' => $config_value));
                    if($editResult === false)
                    {
                        M()->execute('rollback');
                        $this->error('修改失败');
                    }
                }
            }
            //修改系统维护信息
            $data['config_id'] = 3;
            $data['config_value'] = I('system_maintenance');
            if(false === M('system_config')->save($data)){
                M()->execute('rollback');
                $this->error('修改失败');
            }
//            echo M()->getLastSql();eixt;
            M()->execute('commit');
            $this->success('修改成功');
        }
    }
}